//
//  MovieDetailTableViewCell.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/21.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "MovieDetailTableViewCell.h"

@implementation MovieDetailTableViewCell

- (void)dealloc {
    
    [_label release];
    [_titleLabel release];
    [_favorite release];
    [_pic release];
    [_nameLabel release];
    [_timeLabel release];
    [_detailLabel release];
    
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.label = [YHYBaseLabel new];
        _label.backgroundColor = [UIColor colorWithRed:241/255.0 green:233/255.0 blue:238/255.0 alpha:1];
        _label.text = @"  电影故事";
        _label.textColor = [UIColor lightGrayColor];
        _label.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_label];
        
        self.pic = [UIImageView new];
        [self.contentView addSubview:_pic];
        
        self.nameLabel = [YHYBaseLabel new];
        _nameLabel.font = [UIFont systemFontOfSize:14];
        _nameLabel.textColor = [UIColor blueColor];
        [self.contentView addSubview:_nameLabel];

        self.timeLabel = [YHYBaseLabel new];
        _timeLabel.font = [UIFont systemFontOfSize:14];
        _timeLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_timeLabel];

        self.titleLabel = [YHYBaseLabel new];
        _titleLabel.font =[UIFont systemFontOfSize:15];
        [self.contentView addSubview:_titleLabel];

        self.detailLabel = [YHYBaseLabel new];
//        _detailLabel.font = [UIFont systemFontOfSize:13];
        _detailLabel.numberOfLines = 0;
        [self.contentView addSubview:_detailLabel];

        self.favorite = [UIButton buttonWithType:UIButtonTypeSystem];
        [_favorite setImage:[[UIImage imageNamed:@"收藏前"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self.contentView addSubview:_favorite];
        
        
        [_label release];
        [_titleLabel release];
        [_pic release];
        [_nameLabel release];
        [_timeLabel release];
        [_detailLabel release];
        
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _label.frame = CGRectMake(0, 0, INWIDTH, 30);
    
    _pic.frame = CGRectMake(10, _label.frame.size.height + 10, 45, 45);
    _pic.layer.cornerRadius = 45 / 2;
    _pic.layer.masksToBounds = YES;
    
    _nameLabel.frame = CGRectMake(_pic.bounds.size.width + 20, 40, 100, 20);
    
    _timeLabel.frame = CGRectMake(_pic.bounds.size.width + 20, 60, 120, 20);
    
    _favorite.frame = CGRectMake(INWIDTH - 50, _label.frame.size.height + 15, 30, 30);
    
    _titleLabel.frame = CGRectMake(_pic.bounds.size.width + 20, 85, 200, 25);
    
    _height = [[self class]heightWithText:_detailLabel.text];

    _detailLabel.frame = CGRectMake(_pic.bounds.size.width + 20, 100, INWIDTH - 90, _height);
    
}

+ (CGFloat)heightWithText:(NSString *)text {
    if (text == nil) {
        return 0;
    }
    
    
    NSDictionary *fontDic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    CGSize size = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 90, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:fontDic context:nil].size;
    
    return size.height + 50;

}

- (void)setDataModel:(MovieDataModel *)dataModel {
    if (_dataModel != dataModel) {
        [_dataModel release];
        _dataModel = [dataModel retain];
    }
    
    [_pic sd_setImageWithURL:[NSURL URLWithString:dataModel.userNew.web_url]];
    
    _nameLabel.text = dataModel.userNew.user_name;
    
    NSMutableString *string = [NSMutableString stringWithString:dataModel.input_date];
    [string deleteCharactersInRange:NSMakeRange(10, 9)];
    _timeLabel.text = string;
    
    _titleLabel.text = dataModel.title;
    
    _detailLabel.text = dataModel.content;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
